package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.PermissionMapper;
import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.acl.service.RolePermissionService;
import com.atguigu.ssyx.acl.utils.PermissionHelper;
import com.atguigu.ssyx.model.acl.Permission;
import com.atguigu.ssyx.model.acl.RolePermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author suchengchen
 * @date 2023/9/5
 * @description
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private RolePermissionService rolePermissionService;

    /**
     * 查询所有菜单
     *
     * @return
     */
    @Override
    public List<Permission> queryAllPermission() {

        List<Permission> permissions = baseMapper.selectList(null);

        List<Permission> result = PermissionHelper.buildPermission(permissions);

        return result;
    }

    /**
     * 递归删除菜单
     *
     * @param id
     */
    @Override
    public void removeChildById(Long id) {

        List<Long> ids = new ArrayList<>();
        this.getAllPermissionId(id, ids);

        baseMapper.deleteBatchIds(ids);
    }

    private void getAllPermissionId(Long id, List<Long> ids) {
        LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Permission::getPid, id);
        List<Permission> childList = baseMapper.selectList(wrapper);

        childList.forEach(c -> {
            ids.add(c.getId());
            this.getAllPermissionId(c.getId(), ids);
        });
    }

    /**
     * 根据用户id获取菜单权限
     *
     * @param roleId 用户id
     * @return
     */
    @Override
    public List<Permission> getPermissionByRoleId(Long roleId) {

        List<RolePermission> byRoleId = rolePermissionService.getByRoleId(roleId);

        List<Permission> selectedList = new ArrayList<>();

        byRoleId.forEach(item -> {
            Permission permission = baseMapper.selectById(item.getPermissionId());
            selectedList.add(permission);
        });

        List<Long> selectedIds = selectedList.stream().map(Permission::getId).collect(Collectors.toList());

        List<Permission> permissions1 = baseMapper.selectList(null);

        List<Permission> result = PermissionHelper.buildPermission(permissions1);

        setSelect(result, selectedIds);

        return result;
    }

    void setSelect(List<Permission> result, List<Long> selectedIds) {

        result.forEach(c -> {
            if (selectedIds.contains(c.getId())) {
                c.setSelect(true);
            }
            if (c.getChildren() != null) {
                setSelect(c.getChildren(), selectedIds);
            }
        });

    }

    /**
     * 为用户分配菜单权限
     *
     * @param roleId       用户id
     * @param permissionId
     */
    @Override
    public void doAssign(Long roleId, List<Long> permissionId) {

        //先把原来的权限全删掉
        rolePermissionService.removeByRoleId(roleId);

        List<RolePermission> rolePermissions = new ArrayList<>();
        if (permissionId != null) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId((long) 1);
            rolePermissions.add(rolePermission);
        }

        for (Long id : permissionId) {

            RolePermission rolePermission = new RolePermission();
            rolePermission.setPermissionId(id);
            rolePermission.setRoleId(roleId);
            rolePermissions.add(rolePermission);
        }


        rolePermissionService.saveBatch(rolePermissions);

    }

}




















